package com.superhelper.superhelper_web.controller.setting;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.superhelper.common.utils.CryptUtil;
import com.superhelper.superhelper_web.common.CommonHelper;
import com.superhelper.superhelper_web.common.Consts;
import com.superhelper.superhelper_web.controller.base.AbstractController;
import com.superhelper.superhelper_web.domain.Admin;
import com.superhelper.superhelper_web.domain.PageModel;
import com.superhelper.superhelper_web.domain.Resp;
import com.superhelper.superhelper_web.domain.Role;

/**
 * 
 * @author xiejunbo
 *
 */
@Controller
@RequestMapping("admin/setting")
public class SettingController extends AbstractController {

	@RequestMapping("addUser")
	public String addAdmin(ModelMap model) {
		List<Role> roles = adminService.getAllAUserRole();
		model.put("roles", roles);
		return "admin/setting/addAdmin";
	}

	@RequestMapping("editUser")
	public String editAdmin(ModelMap model, Integer id) {
		Admin admin = adminService.getAdminById(id);
		List<Integer> list = adminService.getRoleIdByUserId(id);
		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		for (Integer roleId : list) {
			map.put(roleId, roleId);
		}
		List<Role> roles = adminService.getAllAUserRole();
		for (Role r : roles) {
			if (map.containsKey(r.getId())) {
				r.setChecked(true);
			}
		}
		model.put("roles", roles);
		model.put("admin", admin);
		return "admin/setting/editAdmin";
	}

	/**
	 * 添加管理员
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("saveUser")
	public Resp addUser(Admin admin) {
		Admin adminSession = CommonHelper.getSessionUser();// 从session中获取admin对象
		admin.setCreateId(adminSession.getId());
		boolean result = adminService.addAdmin(admin);
		return result ? success("添加成功!") : fail("网络不稳定,添加失败");
	}

	/**
	 * 编辑管理员
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("updateUser")
	public Resp updateUser(Admin admin) {
		boolean result = adminService.editAdmin(admin);
		return result ? success("修改成功!") : fail("网络不稳定,添加失败");
	}

	/**
	 * 禁止登录管理员
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("lockUser")
	public Resp lockUser(Integer id, Integer status) {
		boolean result = adminService.updateAdminStatus(id, status);
		if (result) {
			return new Resp(Consts.Code.SUCCESS, "操作成功!", status);
		} else {
			return new Resp(Consts.Code.FAIL, "网络不稳定,操作失败!", status);
		}
	}

	/**
	 * 查看管理员
	 * 
	 * @return
	 */
	@RequestMapping("getAdmin")
	public String getAdmin(ModelMap model, HttpServletRequest request,
			PageModel pageModel) {
		Admin adminSession = CommonHelper.getSessionUser();// 从session中获取admin对象
		List<Admin> list = adminService.getAdmin(adminSession.getRoleCode(),
				adminSession.getId(), pageModel);
		model.put("admins", list);
		return "admin/setting/getAdmin";
	}

	/**
	 * 删除管理员
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delAdmin")
	public Resp delAdmin(Integer id) {
		return adminService.delAdminById(id) ? success("删除成功!")
				: fail("网络不稳定,删除失败");
	}

	@RequestMapping("enterPwd")
	public String updateRootPwd(ModelMap model) {
		Admin admin = CommonHelper.getSessionUser();// 从session中获取admin对象
		model.addAttribute("adminId", admin.getId());
		return "admin/setting/updatePwd";
	}

	/**
	 * 更新管理员密码
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("updatePwd")
	public Resp updatePwd(Integer id, String pwd, String newPwd) {
		boolean result = false;
		Admin admin = adminService.getAdminById(id);
		if (admin != null) {
			String md5Pwd = CryptUtil.md5(pwd);
			if (!md5Pwd.equals(admin.getPwd())) {
				return fail("原始密码不正确！");
			}
			result = adminService.updateAdminPwd(id, newPwd);
		}
		return result ? success("保存成功") : fail("网络不稳定,保存失败");
	}

}
